package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117620724";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCg4dznwWD/KnkR97HOSlkrGi2VTj2lgjO+DWYSUUumcyY93+8v3ujS/Q4CAc0k/TFHgv4+e7McYjlAh07Uhr7eMi9H3fyvyvezVLCRKubOS0hCcBmgODXbOQNmtM8Rm9jn69sVwdiUX2gZ7e9ZvIUAAkiEKe8l4BozuoBQBsF5Aaz9GkFh3PiBcRpIzPdsrzFmnSUxIX4GYDVK7jloXF1oR+G6ro6O3ZWvO9CmHiNTm7Yrvw5vZxlUi2uEWfqA4E9sVRMFfsHUC9dO4oPo0S6N7ANu7itxdcUAJVb98IV4g6AS86m2YohQsLlDuQQ0FBUC4ZT/hrovkKvwBKRBPN8tAgMBAAECggEAIi+cZ0q6N75ot56wk6tMw1HuublEdDT0lb/3HpFTx+zUBXXeN4fgZWUMYvTLCL8ch5uYNuLGLpPYopOd9p8jU8fKMDVFin1QoTSvyEkbfl6U3jk/2MHY/9pflKV7ErzRHIcLqNXmFbEoR8o9gA4FHPlfO2ZFwQCb0L4iod6toe6X8QkzGg1um4yxPpDj+OA2Z1+eRwkJIKLya/zD8ZcoZxyCzwnev69frTjwokdLrsUVk+YIReE3P7T8rqaOLE15iWvxapzt6EBjwSBVsZBlfI31FuvPFjeYt997PgCvwrk4SmP9kS1fClZbr5+qU9qgmMipkM28RfknECPtTVeJwQKBgQDs7Hc2qe4123wYwM4GT71sC5JnGrGV0DDkfYMG3DbWyxHnXtgLHD7qVPgZDUmxsHAciwNe496AR9+UpfoRoGJiAKIteVqky4nxqvH6NV0zrZ38iSWmKdlXFNMKo6QMBG91vXvavYBPw2f/SgEMybJnGyMONzeZTRhU7NhB9iRAKQKBgQCt1gLjPu09pgG2W9/daTF2Y/BZ45DiCoF3uF3tIIy6OnDzo44y+cS70Az5wMI+3Nd5QYRZoAWB1VWd0Jn8BB/Yx1QiyfmHcsqxcSCvm7zLLGuOb9J752tUc0cQbxwFgUviNONsMqmGK3YhupEkiTXiDMfunmiyl7xfz/osE8j3ZQKBgCQoiqErGhHaJ3xXoJnR/aTJt1RI/KVSmsOkbCTV4wCxCVB4Zxx6+RcmVJ7uKjsWJGYWyTLoMAL/9R1on1ulUJ6IXjf2d2EDN1BMuJVpI4pJiv0JqVdMt8DUisjq5JR48NHVU1u05CpINkz97F8ZQUL/H0CCKNgargX4B6E5gZrRAoGAZFB6rB51Ez1IAa70+bUpiPNVENpEklyTp72p3hwJebId2ksI1RvTTrpZsopszY/tfBi1wm6Ynvq4BHELWyYaJqOoRscdCu/QTiNFRHFvuiMxEiovYbcC9eHsIWys0/RwEOKKVrbJoxZTflVGSrovcPoFKgsZye3ME9JVwLfOIg0CgYEAzVEWilhvf4cJWTRpTQyXR2U3PK813yfRRctt+zZyoAi9HlnOYH4bqxBBmyqO53te5h8XMALnpcXKqmM8F6mATwkxF/0C7e910Woh/D+yfm0W/YfBzZAB2ePbZxRtDEZ9lFLOY7nCb8hk2tUoaY2DiqO6jw6clZof3P0xnT+04CU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzFrT19EZAXv2gIv6wmksgWoVSciTOmszI/d2S4SALYAvtAA49HqrDM/mgZIglEd5HoS50gBvZwAVcVDwqgcFvgJ8Vr4ycIEByCEOpT6WZM/kblecfny8twmnNNLbE1nFdEdUQfmI/MslSY4VYKvIC/dASU3Hwffl4UMggt6nX/OOFgwQGJxBwm3PcCdnkp3qyuo8DBhxsMClLnHVI36TRV+kqncuQZE2X5tKg4GXdLSxnJwEyf62ol2L2KH5x+QI+wHmbaGq53Djck1rBvUAK8E6LqFpVS6P5cOKMbJCiS4B37mSpmpWbTeiV5Y78kCcJmvr5w8cm/b51MTyKi3UkwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://178lto29xc.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "1m";


    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;
    }
}
